package com.wly.datapool.stage
{
	import com.wly.ActiveManager;
	import com.wly.PanelManager;
	import com.wly.datapool.global.GlobalInfo;
	import com.wly.datapool.monster.MonsterInfo;
	import com.wly.dynamicc.DynamicManager;
	import com.wly.dynamicc.DynamicObject;
	import com.wly.ui.panel.GlobalInfoPanel;
	import com.wly.ui.panel.StageSuccPanel;
	import com.wly.ui.sound.SoundLayer;
	
	import flash.display.MovieClip;
	import flash.display.Stage;

	public class GameStageInfo
	{
		public var mapIndex:int;
		public var batArr:Array = [];
		public var monsterBat:Array = [];
		private var _batStartTick:int;
		private var _dynamicObj:DynamicObject;
		private var _curBat:int;
		private var _curMon:int;
		public var bgSound:MovieClip;
		
		public var grade:int;
		
		public function start():void
		{
			_batStartTick = DynamicManager.TICKCOUNT;
			_curBat    = 0;
			_curMon    = 0;
			
			_dynamicObj = new DynamicObject;
			_dynamicObj.action = onTick;
			DynamicManager.getInstance().registerObj(_dynamicObj);
			GlobalInfoPanel.getInstance().updateData();

			bgSound = new StageBgSound;
			bgSound.stop();
			SoundLayer.getInstance().addChild(bgSound);
			if(GlobalInfo.isPlaySound)
			{
				bgSound.gotoAndStop(2);
			}
		}
		
		public function destory():void
		{
			DynamicManager.getInstance().unRegisterObj(_dynamicObj);
		}
		
		private function get curBat():Array
		{
			return batArr[_curBat] as Array;
		}
		
		private function get readyMonster():MonsterInfo
		{
			if(curBat)
			{
				return curBat[_curMon] as MonsterInfo;
			}
			return null;
		}
		
		public function onTick():void
		{
			//trace(this, "do game stage here");
			if(readyMonster)
			{
				if(readyMonster.startTick == DynamicManager.TICKCOUNT-_batStartTick)
				{
					readyMonster.deadCheck = onMonsterDead;
					readyMonster.start();
					_curMon++;
				}
			}
		}
		
		public function onMonsterDead():void
		{
			trace(this, "onMonsterDead",batCompleteCheck() );
			if(batCompleteCheck())
			{
				_curBat++;
				trace(this, _curBat, batArr.length);
				if(isLastBat)
				{
					doStageComplete();
				}
			}
		}
		
		private function batCompleteCheck():Boolean
		{
			var monster:MonsterInfo;
			var monArr:Array;
			var i:int;
			
			if(curBat)
			{
				for(i=0; i<curBat.length; i++)
				{
					monster = curBat[i] as MonsterInfo;
					trace(monster.isAlive);
					if(monster.isAlive)
					{
						return false;
					}
				}
			}
			
			_curMon = 0;
			_batStartTick = DynamicManager.TICKCOUNT;
			return true;
		}
		
		public function get isLastBat():Boolean
		{
			trace(this, _curBat, batArr.length);
			if(_curBat == batArr.length)
			{
				return true;
			}
			return false;
		}
		
		private function doStageComplete():void
		{
			trace(this, "stage complete");			
			destory();
			var succPanel:StageSuccPanel = new StageSuccPanel();
			succPanel.okCallBack = clear;
			PanelManager.getInstance().showPenelCenter(succPanel);
		}
		
		public function clear():void
		{
			trace(this, "clear stage here");
		}
	}
}